package cn.idongjia.logloader.mongo;

import cn.idongjia.logloader.Param;
import org.mongodb.morphia.geo.LineString;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.validation.ListValueValidator;

import java.util.*;

public class MongoJobLogger extends MongoRepository {

    private Map<String,String> logtrace = new HashMap<>();

    private long ltime = System.currentTimeMillis();

    public static final long MAXSIZE = Long.MAX_VALUE;

    public MongoJobLogger(Properties properties) {
        super(properties);
        setTableName("logForServer");

    }

    public void query(Param param){
        Query<LogInfo> query = template.createQuery(LogInfo.class);
        buildQuery(param,query);
        query.order("_id");
        print(query.asList());
    }


    public List<LogInfo> searchLargeByTm(long tm, Param param){
        Query<LogInfo> query = template.createQuery(LogInfo.class);
        buildQuery(param,query);
        query.field("ltime").greaterThan(tm);
        return query.asList();
    }

    public void tailf(Param param){
        //第一步,查询最后几条记录
        Query<LogInfo> query = template.createQuery(LogInfo.class);
        buildQuery(param,query);
        query.order("_id");
        query.limit(10);
        print(query.asList());

        while (true){
            List<LogInfo> lists = searchLargeByTm(ltime,param);
            print(lists);
        }

//        List list = query.limit(10).asList();
//        if (null != list && !list.isEmpty()){
//            Iterator<LogInfo> logInfos = list.iterator();
//            while (logInfos.hasNext()){
//                LogInfo logInfo = logInfos.next();
//                System.out.println(logInfo);
//            }
//        }
    }

    private void buildQuery(Param param, Query<LogInfo> query) {
        if(null != param.getLogLevel()){
            query.field("logLevel").equal(param.getLogLevel());
        }
        if (null != param.getLogName()){
            query.field("logName").equal(param.getLogName());
        }
        if (null != param.getModuleId()){
            query.field("moduleId").equal(param.getModuleId());
        }
        if (null != param.getServerId()){
            query.field("serverId").equal(param.getServerId());
        }
        if (null != param.getMsg()){
            query.field("msg").contains(param.getMsg());
        }
        if (0 != param.getBt()){
            query.field("ltime").greaterThanOrEq(param.getBt());
        }
        if (0 != param.getEt()){
            query.field("ltime").lessThanOrEq(param.getEt());
        }
        if (0 != param.getLimit()){
            query.limit(param.getLimit());
        }
    }

    public void print(List<LogInfo> list){
        if (null != list && !list.isEmpty()){
            Iterator<LogInfo> logInfos = list.iterator();
            LogInfo logInfo = null;
            while (logInfos.hasNext()){
                logInfo = logInfos.next();
                if(logtrace.containsKey(logInfo.get_id())){
                   continue;
                }
                else {
                   if (logtrace.size() > MAXSIZE){
                       logtrace.clear();
                   }
                   logtrace.put(logInfo.get_id(),"111");
                }
                System.out.println(logInfo);
            }

            ltime = logInfo.getLtime() - 1;
        }
    }
}
